//
//  ContentView.swift
//  TestConcurreces
//
//  Created by rolodestar on 2025/6/16.
//

import SwiftUI
import SwiftData
import BilibiliSearch

struct ContentView: View {
    @Environment(\.modelContext) private var modelContext
    @Query private var items: [Item]

    var body: some View {
        NavigationSplitView {
            List {
                ForEach(items) {
 item in
                    NavigationLink { // 注意这里有个多余的换行，原文件可能排版问题，保留以确保匹配
                        Text("Item at \(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard))")
                            .padding()
                            .background(Color.white)
                            .cornerRadius(10)
                            .shadow(color: .gray.opacity(0.3), radius: 5, x: 0, y: 2)
                    } label: {
                        Text(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard))
                            .padding(8)
                            .background(Color.gray.opacity(0.1))
                            .foregroundColor(.black)
                            .cornerRadius(8)
                            .frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
                    }
                }
                .onDelete(perform: deleteItems)
            }
            .navigationSplitViewColumnWidth(min: 180, ideal: 200)
            .listRowInsets(EdgeInsets())
            .background(Color.gray.opacity(0.05))
            .toolbar {
                ToolbarItem {
                    Button(action: addItem) {
                        Label("Add Item", systemImage: "plus")
                            .padding(8)
                            .background(Color.blue)
                            .foregroundColor(.white)
                            .cornerRadius(8)
                    }
                }
            }
        } detail: {
            Text("Select an item")
                .font(.title)
                .foregroundColor(.secondary)
                .padding()
        }
    }

    private func addItem() {
        withAnimation {
            let newItem = Item(timestamp: Date())
            modelContext.insert(newItem)
        }
    }

    private func deleteItems(offsets: IndexSet) {
        withAnimation {
            for index in offsets {
                modelContext.delete(items[index])
            }
        }
    }
}

#Preview {
    ContentView()
        .modelContainer(for: [
            Item.self,
            BeautyModel.self,
            BilibiliNetVideo.self,
            BilibiliVideoModel.self,
           
//            BilibiliVideoModel.self,
        ], inMemory: true)
//        .modelContainer(for: Item.self, inMemory: true)
}
